<?php
require_once 'auth.php';
requireLogin();

$conn = dbConnect();
$search = $_GET['search'] ?? '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10;
$offset = ($page - 1) * $perPage;

// 获取总记录数
$countSql = "SELECT COUNT(*) as total FROM inventory";
if (!empty($search)) {
    $countSql .= " WHERE product_name LIKE '%" . $conn->real_escape_string($search) . "%'";
}
$countResult = $conn->query($countSql);
$totalItems = $countResult->fetch_assoc()['total'];
$totalPages = ceil($totalItems / $perPage);

// 获取分页数据
$sql = "SELECT * FROM inventory";
if (!empty($search)) {
    $sql .= " WHERE product_name LIKE '%" . $conn->real_escape_string($search) . "%'";
}
$sql .= " ORDER BY last_updated DESC LIMIT $offset, $perPage";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>库存管理</title>
    <link rel="stylesheet" href="/dist/css/tabler.min.css">
</head>
<body class="layout-fluid layout-vertical">
    <?php include 'navbar.php'; ?>
    <div class="page">
        <div class="container-fluid">
            <div class="card mt-3">
                <div class="card-header">
                    <h3 class="card-title">库存清单</h3>
                    <div class="ms-auto">
                        <a href="inventory_add.php" class="btn btn-primary">
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                                <path d="M12 5l0 14" />
                                <path d="M5 12l14 0" />
                            </svg>
                            新增商品
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-vcenter table-hover"> <!-- 添加table-hover类 -->
                            <thead>
                                <tr>
                                    <th>商品名称</th>
                                    <th>SKU</th>
                                    <th>数量</th>
                                    <th>位置</th>
                                    <th>最后更新</th>
                                    <th class="w-1"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($row = $result->fetch_assoc()): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($row['product_name']) ?></td>
                                        <td><?= htmlspecialchars($row['sku']) ?></td>
                                        <td><?= $row['quantity'] ?></td>
                                        <td><?= htmlspecialchars($row['location']) ?></td>
                                        <td><?= date('Y-m-d H:i', strtotime($row['last_updated'])) ?></td>
                                        <td>
                                            <a href="inventory_edit.php?id=<?= $row['id'] ?>" class="btn btn-sm">编辑</a>
                                        </td>
                                    </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- 分页导航 -->
                    <?php include 'pagination.php'; ?>
                </div>
            </div>
        </div>
    </div>

    <script src="/dist/js/tabler.min.js"></script>
</body>

</html>